package com.example.syd.threadtest;

import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.print.PrinterInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

//    class MyThread implements Runnable{
//        @Override
//        public void run(){
//            Log.d(MainActivity.this.toString(),"run");
//        }
//    }
//    private TextView text;
//    public static final int UPDATE_TEXT = 1;

//    private Handler handler = new Handler(){
//        public void handleMessage(Message msg){
//            switch (msg.what){
//                case UPDATE_TEXT:
//                    text.setText("Nice to meet you");
//                    break;
//                default:
//                    break;
//            }
//        }
//    };
    private MyService.DownloadBinder downloadBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
                downloadBinder = (MyService.DownloadBinder)service;
                downloadBinder.startDownload();
                downloadBinder.getProgress();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        //开启线程
//        MyThread myThread = new MyThread();
//        new Thread(myThread).start();
//
//        text = (TextView)findViewById(R.id.text);
//        Button changeText = (Button)findViewById(R.id.change_text);
//        changeText.setOnClickListener(this);

        Button startBtn = (Button)findViewById(R.id.start_service);
        Button stopBtn = (Button)findViewById(R.id.stop_service);
        startBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);

        Button bindBtn = (Button)findViewById(R.id.bind_service);
        Button unbindBtn = (Button)findViewById(R.id.unbind_service);
        bindBtn.setOnClickListener(this);
        unbindBtn.setOnClickListener(this);

        Button startIntentBtn = (Button)findViewById(R.id.start_intent_service);
        startIntentBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start_service:
                Intent startIntent = new Intent(this,MyService.class);
                startService(startIntent);//启动服务
                break;
            case R.id.stop_service:
                Intent stopIntent = new Intent(this,MyService.class);
                stopService(stopIntent);//停止服务
            case R.id.bind_service:
                Intent bindIntent = new Intent(this,MyService.class);
                bindService(bindIntent,connection,BIND_AUTO_CREATE);//绑定服务
                break;
            case R.id.unbind_service:
                unbindService(connection);//解绑服务
                break;
            case R.id.start_intent_service:
                Log.d(MainActivity.this.toString(),"thread is " + Thread.currentThread().getId());
                Intent intentService = new Intent(this,MyIntentService.class);
                startService(intentService);
                break;
            default:
                break;
        }
    }
}
